/**************************
//  CRC16 module
//  Date 2014 Feb 19
//  Authur WU
//  Synchronous preset
**************************/
module rfid_crc16(
     input [23:0] crc16_input,
     input  clk,
     input preset,
     output reg [15:0] crc16_output);

 

function [15:0] crc16_buf;
    input [15:0] crc16;
    input data;

    begin
     crc16_buf[0] = data ^ crc16[15];
      crc16_buf[1] = crc16[0];
   crc16_buf[2] = crc16[1];
   crc16_buf[3] = crc16[2];
   crc16_buf[4] = crc16[3];
      crc16_buf[5] = crc16[4]^crc16[15]^data;
      crc16_buf[6] = crc16[5];
   crc16_buf[7] = crc16[6];
   crc16_buf[8] = crc16[7];
   crc16_buf[9] = crc16[8];
   crc16_buf[10] = crc16[9];
   crc16_buf[11] = crc16[10];
   crc16_buf[12] = crc16[11]^crc16[15]^data;
   crc16_buf[13] = crc16[12];
   crc16_buf[14] = crc16[13];
   crc16_buf[15] = crc16[14];

    end 
 
endfunction

 

function [15:0] crc16_final;
     input [15:0] crc16;
     input [23:0] data;

     integer i;

     begin
       crc16_final= crc16;
        for(i=0;i<24;i=i+1)
          crc16_final = crc16_buf(crc16_final,data[23-i]);
     end

endfunction

always @(posedge clk)
  begin
  if(preset)
    crc16_output<=16'hffff;
  else
     crc16_output<=crc16_final(crc16_output,crc16_input);
  end
endmodule 